package book;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockTryLockTest{

	ReentrantLock lock = new ReentrantLock();
	Condition condition = lock.newCondition();
	
	public void waitMethod(){
		
		try{
			if(lock.tryLock()){
				
				System.out.println(Thread.currentThread().getName() + "获得所");
			}else{
				
				System.out.println(Thread.currentThread().getName() + "没有获得所");
			}
		}finally{
			if(lock.isHeldByCurrentThread()){
				
				System.out.println(Thread.currentThread().getName() + "解锁");
				lock.unlock();
			}
		}
	}

	public static void main(String[] args) throws InterruptedException {
		
		final ReentrantLockTryLockTest lockTask = new ReentrantLockTryLockTest();
		Runnable run = new Runnable() {
			
			@Override
			public void run() {
				lockTask.waitMethod();
			}
		};
		
		Thread t = new Thread(run, "A");
		t.start();
		
		Thread t2 = new Thread(run, "B");
		t2.start();
	}
}
